/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.rest.app.shop.controller.app;

import com.jnzc.ocr.common.bean.Page;
import com.jnzc.ocr.common.bean.PageResult;
import com.jnzc.ocr.common.vo.UserVo;
import com.jnzc.ocr.dependencies.rest.base.BaseController;
import com.jnzc.ocr.rest.app.shop.param.app.AppSupplierCategorySaveParam;
import com.jnzc.ocr.serv.shop.api.ShopSupplierCategoryApi;
import com.jnzc.ocr.serv.shop.entity.ShopSupplierCategory;
import com.jnzc.ocr.serv.shop.filter.ShopSupplierCategoryFilter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhangcm
 * @version 1.0, 2017/9/13
 */
@Api(tags = {"app - 供应商分类接口"})
@RestController
@RequestMapping("/app/supplierCategories")
public class AppSupplierCategoryController extends BaseController {

    @Autowired
    private ShopSupplierCategoryApi shopSupplierCategoryApi;

    @ApiOperation(value = "分页")
    @GetMapping("")
    public PageResult<ShopSupplierCategory> search(Page page){
        UserVo userVo = super.get();
        ShopSupplierCategoryFilter supplierCategoryFilter = new ShopSupplierCategoryFilter();
        supplierCategoryFilter.setShopId(userVo.getShopId());
        return shopSupplierCategoryApi.search(supplierCategoryFilter, page.getPageNo(), page.getPageSize());
    }

    @ApiOperation(value = "详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "供应商分类ID", required = true, paramType = "path")
    })
    @GetMapping("/{id}")
    public ShopSupplierCategory info(@PathVariable Long id){
        UserVo userVo = super.get();
        return shopSupplierCategoryApi.find(id, userVo.getShopId());
    }

    @ApiOperation(value = "保存")
    @PostMapping("")
    public void save(@RequestBody AppSupplierCategorySaveParam param){
        UserVo userVo = super.get();
        ShopSupplierCategory supplierCategoryParam = new ShopSupplierCategory();
        supplierCategoryParam.setId(param.getId());
        supplierCategoryParam.setShopId(userVo.getShopId());
        supplierCategoryParam.setName(param.getName());
        shopSupplierCategoryApi.save(supplierCategoryParam);
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "供应商分类ID", required = true, paramType = "path")
    })
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id){
        UserVo userVo = super.get();
        shopSupplierCategoryApi.delete(id, userVo.getShopId());
    }
}
